<?php

/*
=====================================================
 Delux Board Converstations (DBC) - by -=D_LUX=-
-----------------------------------------------------
 http://dbc-forum.com/
-----------------------------------------------------
 Copyright (c) 2010,2011 Delux Board Conversations (DBC)
=====================================================
 Данный код защищен авторскими правами
=====================================================
*/

session_start();
define('DBC_ROOT', './');
require DBC_ROOT.'engine/common.php';


// If we are logged in, we shouldn't be here
if (!$DBC_user['is_guest'])
{
	header('Location: index.php');
	exit;
}

// Load the register.php language file
require DBC_ROOT.'language/'.$DBC_user['language'].'/register.php';

// Load the register.php/profile.php language file
require DBC_ROOT.'language/'.$DBC_user['language'].'/prof_reg.php';

if ($DBC_config['o_regs_allow'] == '0')
	message($lang_register['No new regs']);

if ($DBC_config['o_captcha_use_registration'] == '1')
	require DBC_ROOT.'engine/recaptchalib.php';

// мод отслеживания ошибок ввода - Visman
if ($DBC_config['o_blocking_time'] != '0')
{
	$block_time_term = time() - 60 * $DBC_config['o_blocking_time'];
	$db->query('DELETE FROM '.$db->prefix.'blocking WHERE block_log < '.$block_time_term) or error('Unable to delete from blocking list', __FILE__, __LINE__, $db->error());
	$block_q = ($DBC_config['o_blocking_reglog'] == '1') ? '(block_type=1 OR block_type=2)' : 'block_type=1';
	$result = $db->query('SELECT block_ip FROM '.$db->prefix.'blocking WHERE block_ip=\''.$db->escape(get_remote_address()).'\' AND '.$block_q) or error('Unable to fetch blocking info', __FILE__, __LINE__, $db->error());
	$block_kolvo = $db->num_rows($result);
	if ($block_kolvo > $DBC_config['o_blocking_kolvo'])
		message($lang_common['Limit of errors']);
}

// User pressed the cancel button
if (isset($_GET['cancel']))
	redirect('index.php', $lang_register['Reg cancel redirect']);


else if ($DBC_config['o_rules'] == '1' && !isset($_GET['agree']) && !isset($_POST['form_sent']))
{
	$page_title = array(DBC_htmlspecialchars($DBC_config['o_board_title']), $lang_register['Register'], $lang_register['Forum rules']);
	define('DBC_ACTIVE_PAGE', 'register');
	define('DBC_REGISTR', 1);
	require DBC_ROOT.'engine/engine.php';
	$link_register = DBC_ROOT. "?do=register";

?>
<table width="100%" height="38" border="0" cellpadding="0" cellspacing="0"><thead><tr>
<td align="left" width="28" class="bg_forum_title_left"><img src="<?php echo $DBC_config['o_base_url'] ?>/engine/skins/images/spacer.gif" border="0"></td>
<td align="left" class="bg_forum_title_center"><span class="cat_title"><?php echo $lang_register['Forum rules'] ?></span></td>
<td align="center" width="28" class="bg_forum_title_right"><img src="<?php echo $DBC_config['o_base_url'] ?>/engine/skins/images/spacer.gif" border="0"></td>
</tr></table>



<table class="forum_adv_table"  width="100%" border="0" cellpadding="0" cellspacing="0"><thead><tr>
<td align="left" class="forum_adv_tleft" width="2"><img src="<?php echo $DBC_config['o_base_url'] ?>/engine/skins/images/spacer.gif" border="0"></td>
<td align="left" width="100%"><div class="forum_adv"><br/>



<div id="rules">
	<div class="box">
		<form method="get" action="register.php">
			<div class="inform">
					<div class="login_info"><?php echo $lang_register['Rules legend'] ?></div>
					<div class="infldset">
						<div class="usercontent"><?php echo $DBC_config['o_rules_message'] ?></div>
					</div>
			<p class="buttons"><input class="fbutton" type="submit" name="agree" value="<?php echo $lang_register['Agree'] ?>" /> <input type="submit" class="fbutton" name="cancel" value="<?php echo $lang_register['Cancel'] ?>" /></p>
			</div>
		</form>
	</div>
</div>


<br/>
</div></td>
<td align="left" class="forum_adv_tright" width="2"><img src="<?php echo $DBC_config['o_base_url'] ?>/engine/skins/images/spacer.gif" border="0"></td>
</td></tr></table>
<table width="100%" height="23" border="0" cellpadding="0" cellspacing="0"><thead><tr>
<td align="left" width="30" class="bg_forum_bott_left"><img src="<?php echo $DBC_config['o_base_url'] ?>/engine/skins/images/spacer.gif" border="0"></td>
<td align="left" class="bg_forum_bott_center"><img src="<?php echo $DBC_config['o_base_url'] ?>/engine/skins/images/spacer.gif" border="0"></td>
<td align="center" width="30" class="bg_forum_bott_right"><img src="<?php echo $DBC_config['o_base_url'] ?>/engine/skins/images/spacer.gif" border="0"></td>
</tr></thead></table>

<?php

	require DBC_ROOT.'engine/footer.php';
}

// Start with a clean slate
$errors = array();

if (isset($_POST['form_sent']))
{
	// Check that someone from this IP didn't register a user within the last hour (DoS prevention)
	$result = $db->query('SELECT 1 FROM '.$db->prefix.'users WHERE registration_ip=\''.get_remote_address().'\' AND registered>'.(time() - 3600)) or error('Unable to fetch user info', __FILE__, __LINE__, $db->error());

	if ($db->num_rows($result))
		message($lang_register['Registration flood']);


	$username = DBC_trim($_POST['req_user']);
	$email1 = strtolower(trim($_POST['req_email1']));
 	
	if ($DBC_config['o_regs_verify'] == '1')
	{
		$email2 = strtolower(trim($_POST['req_email2']));

		$password1 = random_pass(10);
		$password2 = $password1;
	}
	else
	{
		$password1 = DBC_trim($_POST['req_password1']);
		$password2 = DBC_trim($_POST['req_password2']);
	}

	// Validate username and passwords
	check_username($username);

	if (DBC_strlen($password1) < 4)
		$errors[] = $lang_prof_reg['Pass too short'];
	else if ($password1 != $password2)
		$errors[] = $lang_prof_reg['Pass not match'];

	// Validate email
	require DBC_ROOT.'engine/email.php';

	if (!is_valid_email($email1))
		$errors[] = $lang_common['Invalid email'];
	else if ($DBC_config['o_regs_verify'] == '1' && $email1 != $email2)
		$errors[] = $lang_register['Email not match'];

	// Check if it's a banned email address
	if (is_banned_email($email1))
	{
		if ($DBC_config['p_allow_banned_email'] == '0')
			$errors[] = $lang_prof_reg['Banned email'];

		$banned_email = true; // Used later when we send an alert email
	}
	else
		$banned_email = false;

	// Check if someone else already has registered with that email address
	$dupe_list = array();

	$result = $db->query('SELECT username FROM '.$db->prefix.'users WHERE email=\''.$db->escape($email1).'\'') or error('Unable to fetch user info', __FILE__, __LINE__, $db->error());
	if ($db->num_rows($result))
	{
		if ($DBC_config['p_allow_dupe_email'] == '0')
			$errors[] = $lang_prof_reg['Dupe email'];

		while ($cur_dupe = $db->fetch_assoc($result))
			$dupe_list[] = $cur_dupe['username'];
	}

	// Make sure we got a valid language string
	if (isset($_POST['language']))
	{
		$language = preg_replace('#[\.\\\/]#', '', $_POST['language']);
		if (!file_exists(DBC_ROOT.'language/'.$language.'/common.php'))
			message($lang_common['Bad request']);
	}
	else
		$language = $DBC_config['o_default_lang'];

	$timezone = round($_POST['timezone'], 1);
	// мод запоминания пароля - Visman
	$save_pass = (!isset($_POST['save_pass']) || $_POST['save_pass'] != '1') ? '0' : '1';

	$dst = isset($_POST['dst']) ? '1' : '0';

	$email_setting = intval($_POST['email_setting']);
	if ($email_setting < 0 || $email_setting > 2)
		$email_setting = $DBC_config['o_default_email_setting'];

	if ($DBC_config['o_captcha_use_registration'] == '1')
	{
		$result = recaptcha_check_answer($DBC_config['o_captcha_key_private'], get_remote_address(), $_POST['recaptcha_challenge_field'], $_POST['recaptcha_response_field']);
		if (!$result->is_valid)
			$errors[] = $lang_common['Captcha error'];
	}

	// Did everything go according to plan?
	if (empty($errors))
	{
		// Insert the new user into the database. We do this now to get the last inserted ID for later use
		$now = time();

		$intial_group_id = ($DBC_config['o_regs_verify'] == '0') ? $DBC_config['o_default_user_group'] : DBC_UNVERIFIED;
		$password_hash = DBC_hash($password1);

		// Add the user
		$db->query('INSERT INTO '.$db->prefix.'users (username, group_id, password, email, email_setting, timezone, dst, language, style, registered, registration_ip, last_visit) VALUES(\''.$db->escape($username).'\', '.$intial_group_id.', \''.$password_hash.'\', \''.$db->escape($email1).'\', '.$email_setting.', '.$timezone.' , '.$dst.', \''.$db->escape($language).'\', \''.$DBC_config['o_default_style'].'\', '.$now.', \''.get_remote_address().'\', '.$now.')') or error('Unable to create user', __FILE__, __LINE__, $db->error());
		$new_uid = $db->insert_id();

		// If the mailing list isn't empty, we may need to send out some alerts
		if ($DBC_config['o_mailing_list'] != '')
		{
			// If we previously found out that the email was banned
			if ($banned_email)
			{
				$mail_subject = $lang_common['Banned email notification'];
				$mail_message = sprintf($lang_common['Banned email register message'], $username, $email1)."\n";
				$mail_message .= sprintf($lang_common['User profile'], $DBC_config['o_base_url'].'/profile.php?id='.$new_uid)."\n";
				$mail_message .= "\n".'--'."\n".$lang_common['Email signature'];

				DBC_mail($DBC_config['o_mailing_list'], $mail_subject, $mail_message);
			}

			// If we previously found out that the email was a dupe
			if (!empty($dupe_list))
			{
				$mail_subject = $lang_common['Duplicate email notification'];
				$mail_message = sprintf($lang_common['Duplicate email register message'], $username, implode(', ', $dupe_list))."\n";
				$mail_message .= sprintf($lang_common['User profile'], $DBC_config['o_base_url'].'/profile.php?id='.$new_uid)."\n";
				$mail_message .= "\n".'--'."\n".$lang_common['Email signature'];

				DBC_mail($DBC_config['o_mailing_list'], $mail_subject, $mail_message);
			}

			// Should we alert people on the admin mailing list that a new user has registered?
			if ($DBC_config['o_regs_report'] == '1')
			{
				$mail_subject = $lang_common['New user notification'];
				$mail_message = sprintf($lang_common['New user message'], $username, $DBC_config['o_base_url'].'/')."\n";
				$mail_message .= sprintf($lang_common['User profile'], $DBC_config['o_base_url'].'/profile.php?id='.$new_uid)."\n";
				$mail_message .= "\n".'--'."\n".$lang_common['Email signature'];

				DBC_mail($DBC_config['o_mailing_list'], $mail_subject, $mail_message);
			}
		}

		// Must the user verify the registration or do we log him/her in right now?
		if ($DBC_config['o_regs_verify'] == '1')
		{
			// Load the "welcome" template
			$mail_tpl = trim(file_get_contents(DBC_ROOT.'language/'.$DBC_user['language'].'/mail_templates/welcome.tpl'));

			// The first row contains the subject
			$first_crlf = strpos($mail_tpl, "\n");
			$mail_subject = trim(substr($mail_tpl, 8, $first_crlf-8));
			$mail_message = trim(substr($mail_tpl, $first_crlf));

			$mail_subject = str_replace('<board_title>', $DBC_config['o_board_title'], $mail_subject);
			$mail_message = str_replace('<base_url>', $DBC_config['o_base_url'].'/', $mail_message);
			$mail_message = str_replace('<username>', $username, $mail_message);
			$mail_message = str_replace('<password>', $password1, $mail_message);
			$mail_message = str_replace('<login_url>', $DBC_config['o_base_url'].'/login.php', $mail_message);
			$mail_message = str_replace('<board_mailer>', $DBC_config['o_board_title'].' '.$lang_common['Mailer'], $mail_message);

			DBC_mail($email1, $mail_subject, $mail_message);

			message($lang_register['Reg email'].' <a href="mailto:'.$DBC_config['o_admin_email'].'">'.$DBC_config['o_admin_email'].'</a>.', true);
		}

// мод запоминания пароля - Visman
//		DBC_setcookie($new_uid, $password_hash, time() + $DBC_config['o_timeout_visit']);
		DBC_setcookie($new_uid, $password_hash, ($save_pass == '1') ? time() + 31536000 : time() + $DBC_config['o_timeout_visit']);
		
		// удаляем из онлайн таблицы запись для этого пользователя для правильного подсчета макс. кол-во пользователей - Visman
		$db->query('DELETE FROM '.$db->prefix.'online WHERE ident=\''.$db->escape(get_remote_address()).'\'') or error('Unable to delete from online list', __FILE__, __LINE__, $db->error());

		// MOD кэш пользователей - Visman
		if (!defined('FORUM_CACHE_FUNCTIONS_LOADED'))
			require DBC_ROOT.'engine/cache.php';
		generate_users_cache();

		redirect('index.php', $lang_register['Reg complete']);
	}
}

// подключаем скрипт для декодирования - Visman
if ($DBC_config['o_coding_forms'] == '1')
	$page_head['decode64'] = '<script type="text/javascript" src="js/b64.js"></script>';

$page_title = array(DBC_htmlspecialchars($DBC_config['o_board_title']), $lang_register['Register']);
$required_fields = array('req_user' => $lang_common['Username'], 'req_password1' => $lang_common['Password'], 'req_password2' => $lang_prof_reg['Confirm pass'], 'req_email1' => $lang_common['Email'], 'req_email2' => $lang_common['Email'].' 2');
$focus_element = array('register', 'req_user');
define('DBC_ACTIVE_PAGE', 'register');
define('DBC_REGISTR', 1);
require DBC_ROOT.'engine/engine.php';

$timezone = isset($timezone) ? $timezone : $DBC_config['o_default_timezone'];
$dst = isset($dst) ? $dst : $DBC_config['o_default_dst'];
$email_setting = isset($email_setting) ? $email_setting : $DBC_config['o_default_email_setting'];

?>
<?php

// If there are errors, we display them
if (!empty($errors))
{
	// мод отслеживания ошибок ввода - Visman
	if ($DBC_config['o_blocking_time'] != '0')
	{
		$db->query('INSERT INTO '.$db->prefix.'blocking (block_ip, block_log, block_type) VALUES(\''.$db->escape(get_remote_address()).'\', '.time().', 1)') or error('Unable to create blocking', __FILE__, __LINE__, $db->error());

		$block_kolvo = $block_kolvo + 1;
		if ($block_kolvo > $DBC_config['o_blocking_kolvo'] && $DBC_config['o_blocking_user'] != '1')
		{
			$reason = '[MOD] Auto Bloking. Error input register.php'."\n\n".'IP = '.get_remote_address()."\n".'UserName = '.$username."\n".'Email = '.$email1."\n";
			// Should we use the internal report handling?
			if ($DBC_config['o_report_method'] == '0' || $DBC_config['o_report_method'] == '2')
				$db->query('INSERT INTO '.$db->prefix.'reports (post_id, topic_id, forum_id, reported_by, created, message) VALUES(0, 0, 0, '.$DBC_user['id'].', '.time().', \''.$db->escape($reason).'\')' ) or error('Unable to create report', __FILE__, __LINE__, $db->error());

			message($lang_common['Limit of errors']);
		}
	}
	// мод отслеживания ошибок ввода - Visman

?>
<div id="posterror" class="block">
	<h2><span><?php echo $lang_register['Registration errors'] ?></span></h2>
	<div class="box">
		<div class="inbox error-info">
			<p><?php echo $lang_register['Registration errors info'] ?></p>
			<ul class="error-list">
<?php

	foreach ($errors as $cur_error)
		echo "\t\t\t\t".'<li><strong>'.$cur_error.'</strong></li>'."\n";
?>
			</ul>
		</div>
	</div>
</div>

<?php

}
?>
<table width="100%" height="38" border="0" cellpadding="0" cellspacing="0"><thead><tr>
<td align="left" width="28" class="bg_forum_title_left"><img src="<?php echo $DBC_config['o_base_url'] ?>/engine/skins/images/spacer.gif" border="0"></td>
<td align="left" class="bg_forum_title_center"><span class="cat_title"><?php echo $lang_common['Important information'] ?></span></td>
<td align="center" width="28" class="bg_forum_title_right"><img src="<?php echo $DBC_config['o_base_url'] ?>/engine/skins/images/spacer.gif" border="0"></td>
</tr></table>



<table class="forum_adv_table"  width="100%" border="0" cellpadding="0" cellspacing="0"><thead><tr>
<td align="left" class="forum_adv_tleft" width="2"><img src="<?php echo $DBC_config['o_base_url'] ?>/engine/skins/images/spacer.gif" border="0"></td>
<td align="left" width="100%"><div class="forum_adv2">


			<div class="inform_oth">
				<div class="forminfo">
					<p><?php echo $lang_register['Desc 1'] ?></p>
					<p><?php echo $lang_register['Desc 2'] ?></p>
				</div>
			</div>
</div></td>
<td align="left" class="forum_adv_tright" width="2"><img src="<?php echo $DBC_config['o_base_url'] ?>/engine/skins/images/spacer.gif" border="0"></td>
</td></tr></table>
<table width="100%" height="23" border="0" cellpadding="0" cellspacing="0"><thead><tr>
<td align="left" width="30" class="bg_forum_bott_left"><img src="<?php echo $DBC_config['o_base_url'] ?>/engine/skins/images/spacer.gif" border="0"></td>
<td align="left" class="bg_forum_bott_center"><img src="<?php echo $DBC_config['o_base_url'] ?>/engine/skins/images/spacer.gif" border="0"></td>
<td align="center" width="30" class="bg_forum_bott_right"><img src="<?php echo $DBC_config['o_base_url'] ?>/engine/skins/images/spacer.gif" border="0"></td>
</tr></thead></table>



<table width="100%" height="38" border="0" cellpadding="0" cellspacing="0"><thead><tr>
<td align="left" width="28" class="bg_forum_title_left"><img src="<?php echo $DBC_config['o_base_url'] ?>/engine/skins/images/spacer.gif" border="0"></td>
<td align="left" class="bg_forum_title_center"><span class="cat_title"><?php echo $lang_register['Register'] ?></span></td>
<td align="center" width="28" class="bg_forum_title_right"><img src="<?php echo $DBC_config['o_base_url'] ?>/engine/skins/images/spacer.gif" border="0"></td>
</tr></table>



<table class="forum_adv_table"  width="100%" border="0" cellpadding="0" cellspacing="0"><thead><tr>
<td align="left" class="forum_adv_tleft" width="2"><img src="<?php echo $DBC_config['o_base_url'] ?>/engine/skins/images/spacer.gif" border="0"></td>
<td align="left" width="100%"><div class="forum_adv2">



<?php
//            <form id="register" method="post" action="register.php?action=register" onsubmit="this.register.disabled=true;if(process_form(this)){return true;}else{this.register.disabled=false;return false;}">
?>
		<form id="register" method="post" action="register.php?action=register" onsubmit="if(process_form(this)){return true;}else{return false;}">
			<div class="inform">
					<div class="login_info"><?php echo $lang_register['Username legend'] ?></div>
					<div class="infldset">
						<input type="hidden" name="form_sent" value="1" />
						<label class="required"><strong><?php echo $lang_common['Username'] ?> <span><?php echo $lang_common['Required'] ?></span></strong><br /><input type="text" class="input_text" name="req_user" value="<?php if (isset($_POST['req_user'])) echo DBC_htmlspecialchars($_POST['req_user']); ?>" size="25" maxlength="25" /><br /></label>
					</div>
			</div>
<?php if ($DBC_config['o_regs_verify'] == '0'): ?>			<div class="inform">
					<div class="login_info"><?php echo $lang_register['Pass legend'] ?></div>
					<div class="infldset">
						<label class="conl required"><strong><?php echo $lang_common['Password'] ?> <span><?php echo $lang_common['Required'] ?></span></strong><br /><input type="password" class="input_text" name="req_password1" value="<?php if (isset($_POST['req_password1'])) echo DBC_htmlspecialchars($_POST['req_password1']); ?>" size="16" /><br /></label>
						<label class="conl required"><strong><?php echo $lang_prof_reg['Confirm pass'] ?> <span><?php echo $lang_common['Required'] ?></span></strong><br /><input type="password" class="input_text" name="req_password2" value="<?php if (isset($_POST['req_password2'])) echo DBC_htmlspecialchars($_POST['req_password2']); ?>" size="16" /><br /></label>
						<p class="clearb"><?php echo $lang_register['Pass info'] ?></p>
					</div>
			</div>
<?php endif; ?>			<div class="inform">
					<div class="login_info"><?php echo ($DBC_config['o_regs_verify'] == '1') ? $lang_prof_reg['Email legend 2'] : $lang_prof_reg['Email legend'] ?></div>
					<div class="infldset">
<?php if ($DBC_config['o_regs_verify'] == '1'): ?>						<p><?php echo $lang_register['Email info'] ?></p>
<?php endif; ?>						<label class="required"><strong><?php echo $lang_common['Email'] ?> <span><?php echo $lang_common['Required'] ?></span></strong><br />
						<input type="text" name="req_email1" class="input_text"  value="<?php if (isset($_POST['req_email1'])) echo DBC_htmlspecialchars($_POST['req_email1']); ?>" size="50" maxlength="80" /><br /></label>
<?php if ($DBC_config['o_regs_verify'] == '1'): ?>						<label class="required"><strong><?php echo $lang_register['Confirm email'] ?> <span><?php echo $lang_common['Required'] ?></span></strong><br />
						<input type="text" name="req_email2" value="<?php if (isset($_POST['req_email2'])) echo DBC_htmlspecialchars($_POST['req_email2']); ?>" size="50" maxlength="80" /><br /></label>
<?php endif; ?>					</div>



<?php

if ($DBC_config['o_captcha_use_registration'] == '1')
{

?>
				<fieldset>
					<legend><?php echo $lang_register['Captcha legend'] ?></legend>
					<div class="infldset">
						<p><?php echo $lang_register['Captcha info'] ?></p>
						<div class="rbox">
<!-- start recaptcha -->
<?php echo recaptcha_get_html($DBC_config['o_captcha_key_public'])."\n"; ?>
<!-- end recaptcha -->
						</div>
					</div>
				</fieldset>
<?php

}

?>

			<p class="buttons"><input class="fbutton" type="submit" name="register" value="<?php echo $lang_register['Register'] ?>" /></p>
			</div>
		</form>
	</div>

<br/>
</div></td>
<td align="left" class="forum_adv_tright" width="2"><img src="<?php echo $DBC_config['o_base_url'] ?>/engine/skins/images/spacer.gif" border="0"></td>
</td></tr></table>
<table width="100%" height="23" border="0" cellpadding="0" cellspacing="0"><thead><tr>
<td align="left" width="30" class="bg_forum_bott_left"><img src="<?php echo $DBC_config['o_base_url'] ?>/engine/skins/images/spacer.gif" border="0"></td>
<td align="left" class="bg_forum_bott_center"><img src="<?php echo $DBC_config['o_base_url'] ?>/engine/skins/images/spacer.gif" border="0"></td>
<td align="center" width="30" class="bg_forum_bott_right"><img src="<?php echo $DBC_config['o_base_url'] ?>/engine/skins/images/spacer.gif" border="0"></td>
</tr></thead></table>








<?php
require DBC_ROOT.'engine/footer.php';